package com.sisSaude.daos;

import java.sql.ResultSet;
import java.util.Vector;

import com.sisSaude.entityBeans.AvaliacaoDeForcaAbdominal;
import com.sisSaude.utils.DataProvider;
import com.sisSaude.utils.SqlFactory;

public class AvaliacaoDeForcaAbdominalDAO extends GenericDAO<AvaliacaoDeForcaAbdominal> {

	// fields
	public static String SELECT_BASICA = " SELECT AvaliacaoDeForcaAbdominal.*," +
			                             "        Aluno.nome AS nomeDoAluno, " +
			                             "        Funcionario.nome AS nomeDoAvaliador " +
			                             " FROM AvaliacaoDeForcaAbdominal " + 
			                             " INNER JOIN Aluno ON Aluno.id = AvaliacaoDeForcaAbdominal.idAluno " +
			                             " INNER JOIN Funcionario ON Funcionario.id = AvaliacaoDeForcaAbdominal.idAvaliador " ;
	
	// public methods
	public boolean delete(AvaliacaoDeForcaAbdominal avaliacao) {
		String sql = SqlFactory.getSqlDelete("AvaliacaoDeForcaAbdominal", "id");
		DataProvider dataProvider = new DataProvider();
		Vector<Object> params = new Vector<Object>();
		params.add(avaliacao.getId());
		try {
			dataProvider.execute(sql, params);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			dataProvider.close();
		}
		return true;
	}
	public AvaliacaoDeForcaAbdominal get(int id) {
		AvaliacaoDeForcaAbdominal avaliacao = null;
		String sql = SELECT_BASICA + " WHERE AvaliacaoDeForcaAbdominal.id = ? "; 
		DataProvider dataProvider = new DataProvider();
		Vector<Object> params = new Vector<Object>();
		params.add(id);
		try {
			dataProvider.select(sql, params);
			if (dataProvider.next()) {
				avaliacao = new AvaliacaoDeForcaAbdominal();
				loadAttribs(avaliacao, dataProvider.getResultSet());
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			dataProvider.close();
		}
		return avaliacao;
	}
	public void loadAttribs(AvaliacaoDeForcaAbdominal avaliacao, ResultSet r) {
		try {
			avaliacao.setId(r.getInt("id"));
			avaliacao.setIdAluno(r.getInt("idAluno"));
			avaliacao.setIdAvaliador(r.getInt("idAvaliador"));
			avaliacao.setDataDaAvaliacao(r.getDate("dataDaAvaliacao"));
			avaliacao.setTempoDeExecucao(r.getTime("tempoDeExecucao"));
			avaliacao.setExecucoes(r.getInt("execucoes"));
			avaliacao.setNomeDoAluno(r.getString("nomeDoAluno"));
			avaliacao.setNomeDoAvaliador(r.getString("nomeDoAvaliador"));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public AvaliacaoDeForcaAbdominal getUltimaAvaliacao(int idAluno) {
		AvaliacaoDeForcaAbdominal avaliacao = null;
		String sql = SELECT_BASICA + " WHERE AvaliacaoDeForcaAbdominal.idAluno = ? " +
									 " ORDER BY AvaliacaoDeForcaAbdominal.dataDaAvaliacao DESC" +
									 " LIMIT 1"; 
		DataProvider dataProvider = new DataProvider();
		Vector<Object> params = new Vector<Object>();
		params.add(idAluno);
		try {
			dataProvider.select(sql, params);
			if (dataProvider.next()) {
				avaliacao = new AvaliacaoDeForcaAbdominal();
				loadAttribs(avaliacao, dataProvider.getResultSet());
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			dataProvider.close();
		}
		return avaliacao;
	}
	
	// protected methods
	protected boolean insert(AvaliacaoDeForcaAbdominal avaliacao) {
		String sql = SqlFactory.getSqlInsert("AvaliacaoDeForcaAbdominal");
		DataProvider dataProvider = new DataProvider();
		Vector<Object> params = new Vector<Object>();
		params.add(avaliacao.getId());
		params.add(avaliacao.getIdAluno());
	 	params.add(avaliacao.getIdAvaliador());
	 	params.add(avaliacao.getDataDaAvaliacao());
	 	params.add(avaliacao.getTempoDeExecucao());
	 	params.add(avaliacao.getExecucoes());
		try {
			avaliacao.setId(dataProvider.insert(sql, params));
			return avaliacao.getId() > 0;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			dataProvider.close();
		}
	}
	protected boolean update(AvaliacaoDeForcaAbdominal avaliacao) {
		String sql = SqlFactory.getSqlUpdate("AvaliacaoDeForcaAbdominal", "AvaliacaoDeForcaAbdominal.id");
		DataProvider dataProvider = new DataProvider();
		Vector<Object> params = new Vector<Object>();
		params.add(avaliacao.getIdAluno());
	 	params.add(avaliacao.getIdAvaliador());
	 	params.add(avaliacao.getDataDaAvaliacao());
	 	params.add(avaliacao.getTempoDeExecucao());
	 	params.add(avaliacao.getExecucoes());
		params.add(avaliacao.getId());
		try {
			dataProvider.execute(sql, params);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			dataProvider.close();
		}
		return true;
	}

}
